const express = require("express");
const app = express();
const bodyParse=require('body-parser');
const multer  = require('multer');
const appConfig = require('./config/appConfig');
app.use(bodyParse.urlencoded({limit: '2048mb',extended:true }));
app.use(bodyParse.json({limit: '2048mb'}));


const { 
	uploadSingle, 
	uploadBase64
} = require("./controllers/upload");
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
})
app.use(express.static("assets"));
// 使用websocket
require("./controllers/websocket").webSocketServer(app);
app.use(require("./router"))
var upload = multer({ storage: storage })
app.use(require("./miaddleware/header"));
app.get('/jsonp', (req, res) => {
	let data = {
		data: [
			{
				name: "灯鱼",
				age: "12"
			},{
				name: "等鱼",
				age: "78"
			},{
				name: "张三",
				age: "33"
			},{
				name: "李四",
				age: "45"
			},{
				name: "李华",
				age: "21"
			}
		],
		status: 200,
		msg: "操作成功"
	};
	// jsonp 解决方案，拿到前端的函数进行调用把数据传出
	const cb = req.query.cb;
	cbData = `${cb}(${JSON.stringify(data)})`;
	res.send(cbData);
});
app.post("/", (req, res) => {
	let data = {
		data: req.body,
		status: 200,
		msg: "操作成功"
	};
	res.send(data);
});
app.get("/data", (req, res) => {
	console.log(req.query);
	res.send(req.query);
})
app.post("/data", (req, res) => {
	console.log(req.body);
	res.send(req.body);
})
// app.get("/", (req, res) => {
// 	let data = {
// 		data: "hello mockapi",
// 		status: 200,
// 		msg: "操作成功"
// 	};
// 	res.send(data);
// });
app.post('/upload_single', upload.single('file'), uploadSingle)
app.post('/upload_base64', uploadBase64)
app.listen(appConfig.port, () => console.log(`running server to http://localhost:${appConfig.port}`));